const Router = require('@koa/router');
const UserController = require('./app/controller/userController');
const BundleController = require('./app/controller/bundleController');
const ProjectController = require('./app/controller/projectController');
const multer = require('@koa/multer');
const config = require('./app/config/config');

const upload = multer();

const router = new Router({ prefix: config.server.prefix });

/**
 * user route
 */

router.post('/user/login', (ctx, next) => {
    return (new UserController(ctx)).authenticate();
});

/**
 * project
 */

router.get('/project', (ctx, next) => {
    return (new ProjectController(ctx)).list();
});

router.post('/project', (ctx, next) => {
    return (new ProjectController(ctx)).create();
});

router.delete('/project', (ctx, next) => {
    return (new ProjectController(ctx)).delete();
});


/**
 * bundle
 */

router.get('/bundle', (ctx, next) => {
    return (new BundleController(ctx)).list();
});

router.post('/bundle', upload.single('file'), async (ctx, next) => {
    await next();
    return (new BundleController(ctx)).upload();
});

router.delete('/bundle', (ctx, next) => {
    return (new BundleController(ctx)).delete();
});

module.exports = router;
